<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>26. Interpreting the Python keywords</title>
</head>
<body>
<h2 class="title">26. Interpreting the Python keywords</h2>
<p>In the last lesson we saw how we could use the Python interpreter to do
some simple mathematical calculations. In this lesson, we will see how the
Python interpreter deals with some of the Python keywords we have seen within
Reeborg's world.</p>
<!--==============================-->
<hr width="50%">
<h3 class="section">Breaking lines</h3>
<p>Let's start with something simple: enter the expression <tt>3*(2+2)</tt>
at the interpreter prompt and press "enter". The results should look like the
following:</p>
<pre>
&gt;&gt;&gt; 3*(2+2)
12
&gt;&gt;&gt;
</pre>
<p>Now, this time, enter only part of the same expression as written here:
<tt>3*(</tt>, and press "enter". The result should look like the
following:</p>
<pre>
&gt;&gt;&gt; 3*(
...     |
</pre>
<p>where the vertical line "|" represents a blinking cursor. There are a
couple of important things to note:</p>
<ul>
<li>The prompt has changed from <tt>&gt;&gt;&gt;</tt> to <tt>...</tt>. We are
getting a prompt as Python invites us to adding to the text we have entered.
We are getting a <b><i>different</i></b> prompt which is Python's way to tell
us that it thinks that the expression we have entered is not complete.</li>
<li>The cursor is not immediately after the prompt but is moved 4 spaces to
the right. When Python expects that the expression entered is not complete,
it offers to structure the code with some indentation as a suggestion that
<i>perhaps</i> we should add a block of instructions that would be indented
(like we had seen when we defined functions for example.)</li>
</ul>
<p>Let's continue by entering the rest of the expression on the next line and
pressing the "enter" key. Here's the result on my computer:</p>
<pre>
&gt;&gt;&gt; 3*(
...     2+2)
12
&gt;&gt;&gt;
</pre>
<p>Python had concluded that we had entered the complete expression and
calculated it accordingly, giving us the correct result (12) and telling us
it was ready for more input by adding the usual prompt afterwards.</p>
<p>Now, try entering <tt>3*</tt> followed by the "enter" key. Here's the
result I get:</p>
<pre>
&gt;&gt;&gt; 3*
  File <span class="string">"&lt;input&gt;"</span>, line 1
    3*
      ^
SyntaxError: invalid syntax
&gt;&gt;&gt;
</pre>
<p>The difference here is that we had given no indication to Python that we
intended to add more text. In the first case, we had an left opening
parenthese "(" without the corresponding right closing parenthese ")". Python
inferred from this that we intended to add more text, including at least the
")" ... and it gave us the opportunity to do so. In the second case, Python
just assumed that we add no more to add, tried to make sense of what we
wrote, <tt>3*</tt>, and informed us it couldn't.</p>
<p>Now, try to reproduce the example below:</p>
<pre>
&gt;&gt;&gt; 3*(
...     2+2
...
...
...     )
12
&gt;&gt;&gt;
</pre>
<p>As long as Python "thinks" that we have not finished, it will refuse to
calculate the expression and continue to offer us the modified prompt. We can
continue with an other "silly" example:</p>
<pre>
&gt;&gt;&gt; 3*(
...     2+2
...     )+(
...     1+1
...     )
14
&gt;&gt;&gt;
</pre>
<p>which is, admittedly, harder to read and make sense for us than simply
having</p>
<pre>
&gt;&gt;&gt; 3*(2 + 2) + (1 + 1)
14
&gt;&gt;&gt;
</pre>
<p>Now, feel free to try some more examples on your own before reading any
further.</p>
<!--==============================-->
<hr width="50%">
<h3 class="section">Defining functions</h3>
<p>We are now ready to review some of the Python keywords (and concepts!)
that we have seen in Reeborg's world. The first keyword is <span class=
"pykeyword">def</span>. Let's define an admittedly silly function that prints
the number 2. Try to reproduce the result shown below.</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> print2():
...     <span class="keyword">print</span> 2
...    
&gt;&gt;&gt; print2()
2
&gt;&gt;&gt; print2()
2
&gt;&gt;&gt;
</pre>
<p>Now, you have to try it on your own before reading any further.</p>
<p>Notice how helpful Python was to already indent the code within our
function? Notice also how it interpreted an empty line as an indication we
were finished with defining our function. Notice also how it recognizes that
"<tt>print2</tt>", with no spaces between "print" and "2" is different
from<br>
"<tt><span class="pykeyword">print</span> 2</tt>"?</p>
<p>Now, try the following:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> print_many():
...     <span class="keyword">print</span> 3
...     <span class="keyword">print</span> 2
...     <span class="keyword">print</span> 1
...     <span class="keyword">print</span> 0
...    
&gt;&gt;&gt; print_many()
3
2
1
0
&gt;&gt;&gt;
</pre>
<p>As long as we keep adding non-empty lines within the function body, Python
prompts us to add more. As soon as we add an empty line, Python assumes we
are finished. Thus, empty lines can be interpreted as having some meaning by
the Python interpreter; this was not the case in Reeborg's world. It will
also not be the case in programs written outside of Python's interpreter (in
both of these last two cases, empty lines are ignored.) Note that the
behaviour of the Python interpreter in taking an empty line (within a
function definition) is different that what we had seen earlier when we
opened a parenthese without closing it; then, Python "knew" we had more to
add, and it was ignoring empty lines.</p>
<!--==============================-->
<hr width="50%">
<h3 class="section"><span class="pykeyword">if</span> you are confused</h3>
<p>Do you find that what we have just seen about empty lines is confusing?...
Do not despair, you'll quickly get used to it! I'll just keep going with
using other Python keywords that we have seen before, starting with
<span class="pykeyword">if</span>.</p>
<pre>
&gt;&gt;&gt; <span class="keyword">if</span> 5 &gt; 4:
...     <span class="keyword">print</span> 5 &gt; 4
...    
True
&gt;&gt;&gt;
</pre>
<p>Since 5 is greater than 4, Python executes the statement inside the
<span class="pykeyword">if</span> block which is to print <b>the result
of</b><br>
"5 &gt; 4", which is "True". This is probably not what we would want in
general. Try instead the following:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">if</span> 5 &gt; 4:
...     <span class="keyword">print</span> <span class=
"string">"5 &gt; 4"</span>
...    
5 &gt; 4
&gt;&gt;&gt;
</pre>
<p>Surrounding some text by quotes turns it into what is called a
<i>string</i>, which is what Python calls regular text (which it does not try
to calculate). We can also write:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">if</span> 5 &gt; 4:
...     <span class="keyword">print</span> <span class=
"string">'5 is greater than 4'</span>
...    
5 is greater than 4
</pre>
<p>where we have used words instead of a mathematical symbol and, more
importantly, single quotes <tt>(')</tt> instead of double quotes
<tt>(")</tt>
to surround the text; Python gives us the choice to use one or the other as
we wish. This is to allow us to include either single quotes or double quotes
in the text we want to be displayed:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">print</span> "It's easy."
It's easy.
&gt;&gt;&gt; <span class="keyword">print</span> 'He said: "It is easy".'
He said: "It is easy".
</pre>
<p>If we want to include both types of quotes within a string of text, we
must use a backslash <tt>(\)</tt> as an <i>escape</i> character preceding the
quote (or the double quote) that is the same type as the one that begins and
ends the string:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">print</span> 'She said: "It\'s easy".'
She said: "It's easy".
</pre>
<p>Let's go back to <span class="pykeyword">if</span> and include
<span class="pykeyword">else</span> also.</p>
<pre>
&gt;&gt;&gt; <span class="keyword">if</span> 5 &gt; 4:
...    <span class="keyword">print</span> <span class=
"string">"5 is greater than 4"</span>
... <span class="keyword">else</span>:
...    <span class="keyword">print</span> <span class=
"string">"4 is greater than 4"</span>
...    
5 is greater than 4
</pre>
<p>If you look at the display above, there should be no surprise. However,
you should definitely try to reproduce it yourself: you will need to use the
backspace key to align the <span class="pykeyword">else</span> keyword with
the <span class="pykeyword">if</span>.</p>
<p>Make sure you try it before going further.</p>
<!--==============================-->
<hr width="50%">
<h3 class="section"><span class="pykeyword">def</span>ining more
functions.</h3>
<p>We can put the previous code <i>snippet</i> into a useful function:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> greater():
...     <span class="keyword">if</span> 5 &gt; 4:
...         <span class="keyword">print</span> <span class=
"string">"5 is greater than 4"</span>
...     <span class="keyword">else</span>:
...         <span class="keyword">print</span> <span class=
"string">"4 is greater than 5"</span>
...    
&gt;&gt;&gt; <span class="comment"># now let's use it!</span>
&gt;&gt;&gt; greater()
5 is greater than 4
</pre>
<p>You must try to reproduce the above result on your own (and learn to get
the indentation right) before you proceed further.</p>
<p>Now, wouldn't it be nice if we could use <tt>greater()</tt> to compare any
two numbers automatically? To do that, we need to learn about variables.</p>
<center><a href="25-interpreter.htm"><img alt="previous" src=
"../../images/previous.png">Python already knows how to add</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"27-variables.htm">Variables.<img alt="next"
src="../../images/next.png"></a></center>
</body>
</html>
